import logging

# 定义日志记录器类
class CustomLogger:
    def __init__(self, log_file):
        # 创建一个日志器实例
        self.logger = logging.getLogger(__name__)
        # 设置日志级别
        self.logger.setLevel(logging.DEBUG)
        # 创建一个文件处理器
        self.file_handler = logging.FileHandler(log_file, mode='w', encoding='utf-8')
        # 设置文件处理器的日志级别
        self.file_handler.setLevel(logging.DEBUG)
        # 创建一个日志格式器
        self.formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
        # 将格式器添加到文件处理器
        self.file_handler.setFormatter(self.formatter)
        # 将文件处理器添加到日志器
        self.logger.addHandler(self.file_handler)
        # 定义日志级别映射
        self.LOG_LEVEL_MAP = {
            'debug': logging.DEBUG,
            'info': logging.INFO,
            'error': logging.ERROR,
            'warning': logging.WARNING,
            'critical': logging.CRITICAL
        }

    def log(self, level, message):
        # 根据传入的日志级别和消息进行日志记录
        self.logger.log(self.LOG_LEVEL_MAP[level], message)



# 在其他模块中使用这个日志记录器
if __name__ == '__main__':
    # 创建日志记录器实例
    logger = CustomLogger('reporterAdder.log')
    def some_function():
        logger.log('debug', "This is a debug message.")
        logger.log('info', "This is an info message.")
        logger.log('error', "This is an error message.")
        logger.log('warning', "This is a warning message.")
        logger.log('critical', "This is a critical message.")

    # 调用函数并记录日志
    some_function()
